from process import process_log_file, process_folder, delete_processed_files
from get_total_size_count import get_total_size_count

import pandas as pd
import matplotlib.pyplot as plt
import os
import glob
import numpy as np




# 获取c-lop-prediction结果
def get_C_LOP_Prediction_results(comm_size,comm_type):

    if(comm_type == 55):
        return 0

    # lammps 模型参数-> 注意单位
    O = 1.076268
    L = 0.328143 * 0.001

    return O + L * comm_size


    # # 定义模型参数
    # O1 = 0.7996
    # L1 = 0.1980
    
    # O2 = 18.1216
    # L2 = 0.1987

    # if(comm_size <= 32768):
    #     return O1 + L1 * comm_size
    # elif(comm_size > 32768 and comm_size <= 65536):
    #     return O1 + L1 * comm_size
    # elif(comm_size > 65536):
    #     return O2 + L2 * comm_size
def get_C_LOP_Prediction_results(comm_size,comm_type,O,L):

    if(comm_type == 55):
        return 0
    return O + L * comm_size
def add_c_lop_prediction_column_to_csv(file,O,L):
    try:
        print(f"正在处理文件: {file}")
        df = pd.read_csv(file)
        
        # 检查必要的列是否存在
        required_columns = ['total_size', 'count']
        if not all(col in df.columns for col in required_columns):
            print(f"文件 {file} 缺少必要的列: {required_columns}")
        
        # 对每一行单独计算c_lop_prediction
        df['c_lop_prediction'] = df.apply(
            lambda row: get_C_LOP_Prediction_results(row['total_size'],row['comm_type'],O,L) * row['count'], 
            axis=1
        )
        
        # 计算c_lop_prediction的总和
        total_prediction = df['c_lop_prediction'].sum()
        
        # 创建新行，包含总和信息
        new_row = pd.DataFrame({
            'comm_type': ['Total_C-lop_prediction'],
            'count': [''],
            'c_lop_prediction': [total_prediction]
        })
        
        # 将新行添加到DataFrame
        df = pd.concat([df, new_row], ignore_index=True)
        
        # 打印一些示例数据用于验证
        print("\n数据示例：")
        print(df[['total_size', 'count', 'c_lop_prediction']].head())
        print("\n最后一行（总和）：")
        print(df[['total_size', 'count', 'c_lop_prediction']].iloc[-1])
        
        # 保存文件
        output_file = file
        df.to_csv(output_file, index=False)
        print(f"已成功添加 c_lop_prediction 列到文件: {file}")
        
    except Exception as e:
        print(f"处理文件 {file} 时出错: {str(e)}")



if __name__ == "__main__":


    # 1. 处理指定目录下的csv文件，生成新的processed文件，添加有total_size 和 appearance_time 列
    # root_path = r"F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile"        
    root_path = ""
    # 遍历根目录下的所有子文件夹
    if root_path is not None and os.path.exists(root_path):
        for root, dirs, files in os.walk(root_path):
            # 如果当前文件夹包含CSV文件，则处理该文件夹
            if any(file.endswith('.csv') for file in files):
                print(f"\n正在处理文件夹: {root}")
                process_folder(root)
    
    # 2. 根据处理后的文件，统计获取 total_size 和 对应的 appearance_time
    # 所有需要处理的路径列表
    folders = [

        # 1node-2proc
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3643294-1node-2proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3643294-1node-2proc-0-w-20",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3643294-1node-2proc-0-w-30",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3643294-1node-2proc-0-w-40", 
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3643294-1node-2proc-0-w-50",


        # tiny-nodes
        # "f:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3439153-1node-8proc-0-w-50",
        # "f:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3439153-1node-8proc-0-w-100",
        # "f:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3458116-2node-32proc-0-w-50",
        # "f:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3458116-2node-32proc-0-w-100",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/3649009-2node-8proc-0-w-10"

        
        # # small-nodes
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/3493299-2node-32proc-0-w-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/3493299-2node-32proc-0-w-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/3493299-2node-32proc-0-w-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/3493299-2node-32proc-0-w-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/3493299-2node-32proc-0-w-50",
        # "F:/PostGraduate/Point-to-Point-DATA/small-nodes/3649030-4node-16proc-1-w-10"
        
        # # small-nodes - 64 原子
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-4node-64proc-64-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-4node-64proc-64-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-4node-64proc-64-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-4node-64proc-64-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-4node-64proc-64-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-8node-128proc-64-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-8node-128proc-64-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-8node-128proc-64-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-8node-128proc-64-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-8node-128proc-64-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-16node-256proc-64-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-16node-256proc-64-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-16node-256proc-64-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-16node-256proc-64-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-16node-256proc-64-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-32node-512proc-64-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-32node-512proc-64-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-32node-512proc-64-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-32node-512proc-64-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3413576-32node-512proc-64-50",
        
        # # small-nodes - 32 原子
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-4node-64proc-32-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-4node-64proc-32-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-4node-64proc-32-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-4node-64proc-32-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-4node-64proc-32-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-8node-128proc-32-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-8node-128proc-32-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-8node-128proc-32-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-8node-128proc-32-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-8node-128proc-32-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-16node-256proc-32-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-16node-256proc-32-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-16node-256proc-32-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-16node-256proc-32-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-16node-256proc-32-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-32node-512proc-32-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-32node-512proc-32-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-32node-512proc-32-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-32node-512proc-32-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3414147-32node-512proc-32-50",
        
        # # small-nodes - 16 原子
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-4node-64proc-16-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-4node-64proc-16-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-4node-64proc-16-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-4node-64proc-16-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-4node-64proc-16-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-8node-128proc-16-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-8node-128proc-16-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-8node-128proc-16-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-8node-128proc-16-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-8node-128proc-16-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-16node-256proc-16-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-16node-256proc-16-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-16node-256proc-16-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-16node-256proc-16-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-16node-256proc-16-50",
        
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-32node-512proc-16-10",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-32node-512proc-16-20",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-32node-512proc-16-30",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-32node-512proc-16-40",
        # "f:/PostGraduate/Point-to-Point-DATA/small-nodes/lammps-3415271-32node-512proc-16-50",
        
        # # big-nodes
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427930-1024node-16384proc-512-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427930-1024node-16384proc-512-s-100",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427948-512node-8192proc-512-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427948-512node-8192proc-512-s-100",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427970-256node-4096proc-512-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427970-256node-4096proc-512-s-100",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427997-128node-2048proc-512-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3427997-128node-2048proc-512-s-100",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3429036-64node-1024proc-256-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3429036-64node-1024proc-256-s-100",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3429422-32node-512proc-128-s-50",
        # "f:/PostGraduate/Point-to-Point-DATA/big-nodes/LAMMPS/3429422-32node-512proc-128-s-100"

        # all-condition-C-lop-data
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_165511-1node-8proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_165550-1node-16proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_171014-2node-8proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_171238-2node-16proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_171350-2node-24proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_171447-2node-32proc-0-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_174132-4node-8proc-1-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_174222-4node-16proc-1-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_174241-4node-24proc-1-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_174254-4node-32proc-1-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_174302-4node-64proc-1-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192111-8node-8proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192129-8node-16proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192141-8node-24proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192148-8node-32proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192156-8node-64proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192203-8node-96proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250701_192211-8node-128proc-2-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_092607-16node-16proc-4-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_092654-16node-32proc-4-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_092702-16node-64proc-4-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_092715-16node-128proc-4-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_092727-16node-256proc-4-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_094139-32node-32proc-8-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_094152-32node-64proc-8-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_094212-32node-128proc-8-w-10",
        # "F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_094238-32node-256proc-8-w-10",
        #"F:/PostGraduate/Point-to-Point-DATA/tiny-nodes-by-hzpProfile/20250702_094256-32node-512proc-8-w-10",

    ]

    # 创建保存结果的目录
    results_dir = "F:/PostGraduate/Point-to-Point-DATA/deal-data-code/C-lop-Prediction/analysis_results_for_all_condition"

    get_total_size_count(folders, results_dir)

    file = "F:\PostGraduate\Point-to-Point-DATA\deal-data-code\C-lop-Prediction/analysis_results_for_all_condition/20250702_094238-32node-256proc-8-w-10_stats.csv"
    # 3. 根据已经生成csv文件，添加新的列  c_lop_prediction
    O = 1.243645
    L = 0.292645 * 0.001
    add_c_lop_prediction_column_to_csv(file,O,L)

